<template>
  <div style="padding-bottom: 20px;padding-right: 20px;">
    <el-form ref="queryForm" :model="queryParams" :inline="true" label-width="68px" :label-position="'left'">

      <el-form-item class="wenbnen" size="mini" label="项目名称" prop="project_name">
        <el-input v-model="queryParams.project_name" style="width: 130px;" placeholder="请输入项目名称" size="mini" />
      </el-form-item>

      <el-form-item size="small" label="出库单号" prop="warehouse_out_id">
        <el-input v-model="queryParams.warehouse_out_id" style="width: 130px;" placeholder="出库单号" size="mini" />
      </el-form-item>

      <el-form-item size="small" label="业务单号" prop="customer_business_number">
        <el-input v-model="queryParams.customer_business_number" style="width: 130px;" placeholder="客户业务单号" size="mini" />
      </el-form-item>

      <!--      <el-form-item size="small" label="收货方" prop="ship_to">-->
      <!--        <el-input v-model="queryParams.ship_to"  style="width: 130px;" placeholder="收货方" size="mini" />-->
      <!--      </el-form-item>-->

      <!--      <el-form-item size="small" label="发货方" prop="shipper">-->
      <!--        <el-input v-model="queryParams.shipper"  style="width: 130px;" placeholder="发货方" size="mini" />-->
      <!--      </el-form-item>-->

      <el-form-item label="日期范围" prop="date_select_type">
        <el-radio-group v-model="date_select_type">
          <el-radio :label="'今天'">今天</el-radio>
          <el-radio :label="'近7天'">近7天</el-radio>
          <el-radio :label="'近30天'">近30天</el-radio>
          <el-radio :label="'三个月前'">三个月前</el-radio>
          <el-radio :label="'自定义'">自定义</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item
        v-show="date_select_type === '自定义'"
        label="创建日期"
      >
        <el-date-picker
          ref="pick1"
          v-model="queryParams.created_at"
          style="font-size: 12px;"
          value-format="yyyy-MM-dd"
          type="daterange"
          size="mini"
          unlink-panels
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!--    <el-row>-->
    <!--      <el-col :span="1.5">-->
    <!--        <el-col>-->
    <!--          <el-button-->
    <!--            type="primary"-->
    <!--            plain-->
    <!--            icon="el-icon-upload2"-->
    <!--            size="mini"-->
    <!--            @click="exportEdi"-->
    <!--          >导出跟踪表-->
    <!--          </el-button>-->
    <!--        </el-col>-->
    <!--      </el-col>-->
    <!--    </el-row>-->

    <el-table
      ref="table"
      v-loading="loading"
      class="table-wrap"
      :data="tableData"
      row-key="id"
      border
      fit
      header-cell-class-name="header-cell-color"
    >
      <el-table-column
        v-for="(item, index) in bindTableColumns"
        :key="`colTableHeaderTit_${index}`"
        class-name="allowDrag"
        show-overflow-tooltip
        :prop="tableHeaderTit[index].field"
        :label="tableHeaderTit[index].label"
        align="center"
      />
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.statusStr == '全部出库' && scope.row.edi_return == '未回传'"
            size="mini"
            type="success"
            @click="handleReturn(scope.row)"
          >回传</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.page"
      :limit.sync="queryParams.limit"
      size="mini"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { warehouseOutList } from '@/api/storehouse'
import { ediDataReturn, exportEdiWarehouseOut } from '@/api/edi'
import { parseTime } from '@/utils'
import { exportStock } from '@/api/stock'
export default {
  name: 'Outbound',
  data() {
    return {
      date_select_type: '近30天',
      tableHeaderTit: [
        { label: '出库主单编号', field: 'warehouse_out_id', isShow: true, minWidth: 50 },
        { label: '项目/委托方', field: 'project_name', isShow: true, minWidth: 50 },
        { label: '客户业务单号', field: 'customer_business_number', isShow: true, minWidth: 50 },
        { label: '运输方式', field: 'transport_type', isShow: true, minWidth: 50 },
        { label: '商品总数量', field: 'goods_number', isShow: true, minWidth: 50 },
        { label: '出库数量', field: 'actual_number', isShow: true, minWidth: 50 },
        { label: '发货方', field: 'shipper_relation_name', isShow: true, minWidth: 50 },
        { label: '收货方', field: 'consignee_relation_name', isShow: true, minWidth: 50 },
        { label: '创建时间', field: 'created_at', isShow: true, minWidth: 50 },
        { label: '出库状态', field: 'statusStr', isShow: true, minWidth: 50 },
        { label: '发布人', field: 'pub_name', isShow: true, minWidth: 50 },
        { label: 'edi状态', field: 'edi_return', isShow: true, minWidth: 50 }
      ],
      colTableHeaderTit: [
        { label: '出库主单编号', field: 'warehouse_out_id', isShow: true, minWidth: 50 },
        { label: '项目/委托方', field: 'project_name', isShow: true, minWidth: 50 },
        { label: '客户业务单号', field: 'customer_business_number', isShow: true, minWidth: 50 },
        { label: '运输方式', field: 'transport_type', isShow: true, minWidth: 50 },
        { label: '商品总数量', field: 'goods_number', isShow: true, minWidth: 50 },
        { label: '出库数量', field: 'actual_number', isShow: true, minWidth: 50 },
        { label: '发货方', field: 'shipper_relation_name', isShow: true, minWidth: 50 },
        { label: '收货方', field: 'consignee_relation_name', isShow: true, minWidth: 50 },
        { label: '创建时间', field: 'created_at', isShow: true, minWidth: 50 },
        { label: '出库状态', field: 'statusStr', isShow: true, minWidth: 50 },
        { label: '发布人', field: 'pub_name', isShow: true, minWidth: 50 },
        { label: 'edi状态', field: 'edi_return', isShow: true, minWidth: 50 }
      ],
      tableData: [],
      pickerOptions: {
        shortcuts: [{
          text: '今天',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime())
            end.setTime(end.getTime() + 3600 * 1000 * 24)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一周',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '三个月前',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            end.setTime(end.getTime() - 3600 * 1000 * 24 * 90)
            start.setTime(Date.parse('2000/01/01'))
            picker.$emit('pick', [start, end])
          }
        }]
      },
      loading: true,
      queryParams: {
        page: 1,
        limit: 20,
        supplier_name: undefined
      },
      total: 0
    }
  },
  computed: {
    bindTableColumns() { // 表格展示的字段
      const res = this.colTableHeaderTit.filter((v) => {
        return v.isShow == true
      })
      this.tableHeaderTit = JSON.parse(JSON.stringify(res))
      return res
    },
    checkedTableColumns: {
      get() {
        return this.bindTableColumns.map((v) => v.label)
      },
      set(value) {
        this.nimabi = false
        this.colTableHeaderTit.forEach((v) => {
          if (value.includes(v.label)) {
            v.isShow = true
          } else {
            v.isShow = false
          }
        })
      }
    }
  },
  watch: {
    'date_select_type': {
      handler(val) {
        switch (val) {
          case '今天':
            var end = new Date()
            var start = new Date()
            start = parseTime(start.getTime(), '{y}-{m}-{d}')
            end = parseTime(end.getTime() + 3600 * 1000 * 24, '{y}-{m}-{d}')
            this.$set(this.queryParams, 'created_at', [start, end])
            break
          case '近7天':
            var end = new Date()
            var start = new Date()
            start = parseTime(start.getTime() - 3600 * 1000 * 24 * 6, '{y}-{m}-{d}')
            end = parseTime(end.getTime() + 3600 * 1000 * 24, '{y}-{m}-{d}')
            this.$set(this.queryParams, 'created_at', [start, end])
            break
          case '近30天':
            var end = new Date()
            var start = new Date()
            start = parseTime(start.getTime() - 3600 * 1000 * 24 * 29, '{y}-{m}-{d}')
            end = parseTime(end.getTime() + 3600 * 1000 * 24, '{y}-{m}-{d}')
            this.$set(this.queryParams, 'created_at', [start, end])
            break
          case '三个月前':
            var end = new Date()
            var start = new Date()
            end = parseTime(end.getTime() - 3600 * 1000 * 24 * 89, '{y}-{m}-{d}')
            start = parseTime(Date.parse('2000/01/01'), '{y}-{m}-{d}')
            this.$set(this.queryParams, 'created_at', [start, end])
            break
        }
      },
      deep: true, // 深度监听
      immediate: true // 初次监听即执行
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询清单列表 */
    getList() {
      this.loading = true
      // console.log(this.queryParams)
      warehouseOutList(this.queryParams).then(response => {
        this.loading = false
        if (response.code == 10000) {
          if (response.data.length > 0) {
            response.data.forEach(item => {
              if (item.status == '0') { item['statusStr'] = '未出库' }
              if (item.status == '1') { item['statusStr'] = '全部出库' }
              if (item.status == '2') { item['statusStr'] = '部分出库' }
              item['edi_return'] = item['edi_return'] == 0 ? '未回传' : '已回传'
            })
          }
          this.tableData = response.data
          this.total = response.count
        }
      })
    },
    exportEdi() {
      const that = this
      that.loading = true
      let params = {}
      // exportType 1:选中导出 2：列表导出
      // if (that.ids.length > 0) {
      //   params.ids = that.ids.toString()
      // } else {
      //   params = that.queryParams
      // }

      params = that.queryParams

      exportEdiWarehouseOut(params).then(response => {
        // console.log(response)
        if (response.code === 10000) {
          var url = response.data.url.replace('127.0.0.1:8080', 'cross.qekj.net')
          window.open(url)
        } else {
          that.$message.error(response.message)
        }
        that.loading = false
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        page: 1,
        limit: 20,
        supplier_name: undefined
      }
      this.handleQuery()
    },
    handleReturn(row) {
      this.$confirm('正在进行edi操作, 请确认?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.loading = false
        ediDataReturn({ type: row.customer_order_type, record_id: row.id }).then(response => {
          this.loading = false
          if (response.code == 10000) {
            this.$message({
              type: 'success',
              message: '操作成功'
            })
            this.getList()
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消操作'
        })
      })
    }
  }
}
</script>

<style scoped>

</style>
